package com.java.util;


import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

import com.sun.mail.util.MailSSLSocketFactory;

public class EmailUtil {

    // 发件人电子邮箱
    private static String from = null;
    private static String password = null;
    // 指定发送邮件的主机
    private static String host = null;
    // 指定本服务器地址
    private static String localhost = null;

    static {
        final Properties p = new Properties();
        final InputStream is = EmailUtil.class.getClassLoader().getResourceAsStream("email.properties");
        try {
            p.load(is);
            from = p.getProperty("from");
            password = p.getProperty("password");
            host = p.getProperty("host");
            localhost = p.getProperty("localhost");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static boolean send(String emailAddressTo, String code) throws GeneralSecurityException {
        // 获取系统属性
        Properties properties = System.getProperties();

        // 设置邮件服务器
        properties.setProperty("mail.smtp.host", host);

        properties.put("mail.smtp.auth", "true");

        properties.setProperty("mail.debug", "false");//启用调试
        properties.setProperty("mail.smtp.timeout", "1000");//设置链接超时
        properties.setProperty("mail.smtp.port", "465");//设置端口
        properties.setProperty("mail.smtp.socketFactory.port", "465");//设置ssl端口
        properties.setProperty("mail.smtp.socketFactory.fallback", "false");
        properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

        // 关于QQ邮箱，还要设置SSL加密，加上以下代码即可
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        properties.put("mail.smtp.ssl.enable", "true");
        properties.put("mail.smtp.ssl.socketFactory", sf);

        // 获取默认的 Session 对象。
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(from, password);
            }
        });

        try {
            // 创建默认的 MimeMessage 对象。
            MimeMessage message = new MimeMessage(session);

            // Set From: 头部头字段
            message.setFrom(new InternetAddress(from));

            // Set To: 头部头字段
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(emailAddressTo));

            // Set Subject: 头字段
            message.setSubject("快递 e 栈邮箱验证");

            // 发送 HTML 消息, 可以插入html标签
            final long time = new Date().getTime();
            message.setContent("<table style=\"margin:0;padding:0\" width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#494c50\"><tr><td align=\"center\" background=\"" + localhost + "/email/images/bg.jpg\" style=\"background-size:cover;background-position:top\"><table width=\"600\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\"><tr><td height=\"60\"></td></tr><tr><td align=\"center\"><table align=\"center\" bgcolor=\"#FFFFFF\" style=\"border-radius:4px;box-shadow:0 3px 0 #d4d2d2\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td height=\"50\"></td></tr><tr><td align=\"center\" style=\"line-height:0\"><img style=\"display:block;line-height:0;font-size:0;border:0\" src=\"" + localhost + "/email/images/logo.png\" width=\"50\" height=\"26\" alt=\"logo\"/></td></tr><tr><td height=\"15\"></td></tr><tr><td align=\"center\" style=\"font-family:'Open Sans',Arial,sans-serif;font-size:12px;color:#3b3b3b;line-height:26px;text-transform:uppercase;letter-spacing:2px;font-weight:normal\">开课吧 · 新职课 · 2021-08-05</td></tr><tr><td height=\"40\"></td></tr><tr><td align=\"center\" bgcolor=\"#f3f3f3\"><table align=\"center\" class=\"table-inner\" width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td height=\"50\"></td></tr><tr><td align=\"center\" style=\"font-family:'Open Sans',Arial,sans-serif;font-size:36px;color:#3b3b3b;font-weight:bold;letter-spacing:4px\">验证邮箱地址</td></tr><tr><td align=\"center\"><table width=\"25\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td height=\"15\" style=\"border-bottom:2px solid #3cb2d0\"></td></tr></table></td></tr><tr><td height=\"20\"></td></tr><tr><td align=\"center\" style=\"font-family:'Open Sans',Arial,sans-serif;font-size:13px;color:#7f8c8d;line-height:30px\">欢迎您登录快递 e 栈。在开始之前，请先验证邮箱地址的准确性，以便将来可以更改密码。 如果您不知道为什么收到这封邮件，可能是其他人误输入了您的电子邮箱地址，这并没有关系，请忽略它。本邮件5分钟内有效。</td></tr><tr><td height=\"50\"></td></tr></table></td></tr><tr><td height=\"40\"></td></tr><tr><td align=\"center\"><table align=\"center\" bgcolor=\"#3cb2d0\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\" border-radius:4px;box-shadow:0 2px 0 #dedfdf\"><tr><td height=\"55\" align=\"center\" style=\"cursor:pointer;font-family:'Open Sans',Arial,sans-serif;font-size:16px;color:#7f8c8d;line-height:30px;font-weight:bold;padding-left:25px;padding-right:25px\"><a href='" + localhost + "/email/autoVerify.do?email=" + emailAddressTo + "&code=" + code + "&time=" + time + "' style=\"text-decoration:none;color:#fff;display:inline-block;width:75px;height:30px;padding:13px 20px 13px 20px;user-select:none;-webkit-user-select:none;-moz-user-select:none\">立即验证</a></td></tr></table></td></tr><tr><td align=\"center\" style=\" border-bottom-left-radius:4px;border-bottom-right-radius:4px\"><table align=\"center\" class=\"table-inner\" width=\"290\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td height=\"25\"></td></tr><tr><td align=\"center\"><table width=\"80\" border=\"0\" align=\"left\" cellpadding=\"0\" cellspacing=\"0\"><tr><td align=\"center\" style=\"font-family:'Open Sans',Arial,sans-serif;font-size:13px;color:#7f8c8d;line-height:30px\"><a style=\"color:#95a5a6;text-decoration:none\" href=\"" + localhost + "\" target=\"_blank\">快递 e 栈</a></td></tr></table><table width=\"1\" height=\"15\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" align=\"left\"><tr><td height=\"15\" style=\"font-size:0;line-height:0;border-collapse:collapse\"><p style=\"padding-left:24px\">&nbsp;</p></td></tr></table><table width=\"80\" border=\"0\" align=\"left\" cellpadding=\"0\" cellspacing=\"0\"><tr><td align=\"center\" style=\"font-family:'Open Sans',Arial,sans-serif;font-size:13px;color:#7f8c8d;line-height:30px\"><a style=\"color:#95a5a6;text-decoration:none\" href=\"https://www.kaikeba.com/\">kaikeba</a></td></tr></table><table width=\"1\" height=\"15\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" align=\"left\"><tr><td height=\"15\" style=\"font-size:0;line-height:0;border-collapse:collapse\"><p style=\"padding-left:24px\">&nbsp;</p></td></tr></table><table width=\"80\" border=\"0\" align=\"right\" cellpadding=\"0\" cellspacing=\"0\"><tr><td align=\"center\" style=\"font-family:'Open Sans',Arial,sans-serif;font-size:13px;color:#7f8c8d;line-height:30px\"><a style=\"color:#95a5a6;text-decoration:none\" href=\"http://666.zaixianke.cn\">关于我们</a></td></tr></table></td></tr></table></td></tr><tr><td height=\"30\"></td></tr></table></td></tr><tr><td height=\"30\"></td></tr><tr><td align=\"center\" style=\"height:60px;font-family:'Open Sans',Arial,sans-serif;font-size:13px;color:#ffffff;line-height:20px\">&copy; 2021 <a style=\"text-decoration:none;color:#1E9FFF\" href=\"#\">Liu Guangxian</a> . All Rights Reserved.</td></tr><tr><td height=\"35\"></td></tr></table></td></tr></table>",
                    "text/html;charset=utf-8");

            // 发送消息
            Transport.send(message);
            System.out.println(DateUtil.format(new Date()) + " INFO [com.java.util.EmailUtil] Email sent successfully! (to " + emailAddressTo + ')');
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return false;
    }
}
//index
//<table style="margin:0;padding:0" width="100%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#494c50"><tr><td align="center" background="" + localhost + "/email/images/bg.jpg" style="background-size:cover;background-position:top"><table width="600" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td height="60"></td></tr><tr><td align="center"><table align="center" bgcolor="#FFFFFF" style="border-radius:4px;box-shadow:0 3px 0 #d4d2d2" width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td height="50"></td></tr><tr><td align="center" style="line-height:0"><img style="display:block;line-height:0;font-size:0;border:0" src="" + localhost + "/email/images/logo.png" width="50" height="26" alt="logo"/></td></tr><tr><td height="15"></td></tr><tr><td align="center" style="font-family:'Open Sans',Arial,sans-serif;font-size:12px;color:#3b3b3b;line-height:26px;text-transform:uppercase;letter-spacing:2px;font-weight:normal">开课吧 · 新职课 · 2021-08-05</td></tr><tr><td height="40"></td></tr><tr><td align="center" bgcolor="#f3f3f3"><table align="center" class="table-inner" width="500" border="0" cellspacing="0" cellpadding="0"><tr><td height="50"></td></tr><tr><td align="center" style="font-family:'Open Sans',Arial,sans-serif;font-size:36px;color:#3b3b3b;font-weight:bold;letter-spacing:4px">验证邮箱地址</td></tr><tr><td align="center"><table width="25" border="0" cellspacing="0" cellpadding="0"><tr><td height="15" style="border-bottom:2px solid #3cb2d0"></td></tr></table></td></tr><tr><td height="20"></td></tr><tr><td align="center" style="font-family:'Open Sans',Arial,sans-serif;font-size:13px;color:#7f8c8d;line-height:30px">欢迎您登录快递 e 栈。在开始之前，请先验证邮箱地址的准确性，以便将来可以更改密码。 如果您不知道为什么收到这封邮件，可能是其他人误输入了您的电子邮箱地址，这并没有关系，请忽略它。本邮件15分钟内有效。</td></tr><tr><td height="50"></td></tr></table></td></tr><tr><td height="40"></td></tr><tr><td align="center"><table align="center" bgcolor="#3cb2d0" border="0" cellspacing="0" cellpadding="0" style=" border-radius:4px;box-shadow:0 2px 0 #dedfdf"><tr><td height="55" align="center" style="cursor:pointer;font-family:'Open Sans',Arial,sans-serif;font-size:16px;color:#7f8c8d;line-height:30px;font-weight:bold;padding-left:25px;padding-right:25px"><a href='" + localhost + "/email/index.do?code=" + code + "' style="text-decoration:none;color:#fff;display:inline-block;width:75px;height:30px;padding:13px 20px 13px 20px;user-select:none;-webkit-user-select:none;-moz-user-select:none">立即验证</a></td></tr></table></td></tr><tr><td align="center" style=" border-bottom-left-radius:4px;border-bottom-right-radius:4px"><table align="center" class="table-inner" width="290" border="0" cellspacing="0" cellpadding="0"><tr><td height="25"></td></tr><tr><td align="center"><table width="80" border="0" align="left" cellpadding="0" cellspacing="0"><tr><td align="center" style="font-family:'Open Sans',Arial,sans-serif;font-size:13px;color:#7f8c8d;line-height:30px"><a style="color:#95a5a6;text-decoration:none" href="http://666.zaixianke.cn" target="_blank">快递 e 栈</a></td></tr></table><table width="1" height="15" border="0" cellpadding="0" cellspacing="0" align="left"><tr><td height="15" style="font-size:0;line-height:0;border-collapse:collapse"><p style="padding-left:24px">&nbsp;</p></td></tr></table><table width="80" border="0" align="left" cellpadding="0" cellspacing="0"><tr><td align="center" style="font-family:'Open Sans',Arial,sans-serif;font-size:13px;color:#7f8c8d;line-height:30px"><a style="color:#95a5a6;text-decoration:none" href="https://www.kaikeba.com/">kaikeba</a></td></tr></table><table width="1" height="15" border="0" cellpadding="0" cellspacing="0" align="left"><tr><td height="15" style="font-size:0;line-height:0;border-collapse:collapse"><p style="padding-left:24px">&nbsp;</p></td></tr></table><table width="80" border="0" align="right" cellpadding="0" cellspacing="0"><tr><td align="center" style="font-family:'Open Sans',Arial,sans-serif;font-size:13px;color:#7f8c8d;line-height:30px"><a style="color:#95a5a6;text-decoration:none" href="http://666.zaixianke.cn">关于我们</a></td></tr></table></td></tr></table></td></tr><tr><td height="30"></td></tr></table></td></tr><tr><td height="30"></td></tr><tr><td align="center" style="height:60px;font-family:'Open Sans',Arial,sans-serif;font-size:13px;color:#ffffff;line-height:20px">© 2021 <a style="text-decoration:none;color:#1E9FFF" href="#">Liu Guangxian</a> . All Rights Reserved.</td></tr><tr><td height="35"></td></tr></table></td></tr></table>
//success
//<table style="margin:0;padding:0" width="100%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#494c50"><tr><td align="center" background="" + localhost + "/email/images/bg.jpg" style="background-size:cover;background-position:top"><table width="600" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td height="60"></td></tr><tr><td align="center"><table align="center" bgcolor="#FFFFFF" style="border-radius:4px;box-shadow:0 3px 0 #d4d2d2" width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td height="50"></td></tr><tr><td align="center" style="line-height:0"><img style="display:block;line-height:0;font-size:0;border:0" src="" + localhost + "/email/images/logo.png" width="50" height="26" alt="logo"/></td></tr><tr><td height="15"></td></tr><tr><td align="center" style="font-family:'Open Sans',Arial,sans-serif;font-size:12px;color:#3b3b3b;line-height:26px;text-transform:uppercase;letter-spacing:2px;font-weight:normal">开课吧 · 新职课 · 2021-08-05</td></tr><tr><td height="40"></td></tr><tr><td align="center" bgcolor="#f3f3f3"><table align="center" width="500" border="0" cellspacing="0" cellpadding="0"><tr><td height="50"></td></tr><tr><td align="center" style="font-family:'Open Sans',Arial,sans-serif;font-size:36px;color:#3b3b3b;font-weight:bold;letter-spacing:4px">验证成功</td></tr><tr><td align="center"><table width="25" border="0" cellspacing="0" cellpadding="0"><tr><td height="15" style="border-bottom:2px solid #3cb2d0"></td></tr></table></td></tr><tr><td height="20"></td></tr><tr><td align="center" style="font-family:'Open Sans',Arial,sans-serif;font-size:13px;color:#7f8c8d;line-height:30px">不出意外的话，您的刚刚的登录页已经跳转到主页了。如果没有跳转，请直接点击登录按钮，无需输入验证码。 请立即更改密码或账号名以免再次出现账号异常的情况。</td></tr><tr><td height="50"></td></tr></table></td></tr><tr><td height="40"></td></tr><tr><td align="center"><table align="center" bgcolor="#3cb2d0" border="0" cellspacing="0" cellpadding="0" style=" border-radius:4px;box-shadow:0 2px 0 #dedfdf"><tr><td height="55" align="center" style="cursor:pointer;font-family:'Open Sans',Arial,sans-serif;font-size:16px;color:#7f8c8d;line-height:30px;font-weight:bold"><a style="text-decoration:none;color:#fff;display:inline-block;width:75px;height:30px;padding:13px 20px 13px 20px;user-select:none;-webkit-user-select:none;-moz-user-select:none" href="" + localhost + "">快递 e 栈</a></td></tr></table></td></tr><tr><td align="center" style=" border-bottom-left-radius:4px;border-bottom-right-radius:4px"><table align="center" class="table-inner" width="290" border="0" cellspacing="0" cellpadding="0"><tr><td height="25"></td></tr><tr><td align="center"><table width="80" border="0" align="left" cellpadding="0" cellspacing="0"><tr><td align="center" style="font-family:'Open Sans',Arial,sans-serif;font-size:13px;color:#7f8c8d;line-height:30px"><a style="color:#95a5a6;text-decoration:none" href="http://666.zaixianke.cn" target="_blank">快递 e 栈</a></td></tr></table><table width="1" height="15" border="0" cellpadding="0" cellspacing="0" align="left"><tr><td height="15" style="font-size:0;line-height:0;border-collapse:collapse"><p style="padding-left:24px">&nbsp;</p></td></tr></table><table width="80" border="0" align="left" cellpadding="0" cellspacing="0"><tr><td align="center" style="font-family:'Open Sans',Arial,sans-serif;font-size:13px;color:#7f8c8d;line-height:30px"><a style="color:#95a5a6;text-decoration:none" href="https://www.kaikeba.com/">kaikeba</a></td></tr></table><table width="1" height="15" border="0" cellpadding="0" cellspacing="0" align="left"><tr><td height="15" style="font-size:0;line-height:0;border-collapse:collapse"><p style="padding-left:24px">&nbsp;</p></td></tr></table><table width="80" border="0" align="right" cellpadding="0" cellspacing="0"><tr><td align="center" style="font-family:'Open Sans',Arial,sans-serif;font-size:13px;color:#7f8c8d;line-height:30px"><a style="color:#95a5a6;text-decoration:none" href="http://666.zaixianke.cn">关于我们</a></td></tr></table></td></tr></table></td></tr><tr><td height="30"></td></tr></table></td></tr><tr><td height="30"></td></tr><tr><td align="center" style="height:60px;font-family:'Open Sans',Arial,sans-serif;font-size:13px;color:#ffffff;line-height:20px">© 2021 <a style="text-decoration:none;color:#1E9FFF" href="#">Liu Guangxian</a> . All Rights Reserved.</td></tr><tr><td height="35"></td></tr></table></td></tr></table>
//failed
//<table style="margin:0;padding:0" width="100%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#494c50"><tr><td align="center" background="" + localhost + "/email/images/bg.jpg" style="background-size:cover;background-position:top"><table width="600" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td height="60"></td></tr><tr><td align="center"><table align="center" bgcolor="#FFFFFF" style="border-radius:4px;box-shadow:0 3px 0 #d4d2d2" width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td height="50"></td></tr><tr><td align="center" style="line-height:0"><img style="display:block;line-height:0;font-size:0;border:0" src="" + localhost + "/email/images/logo.png" width="50" height="26" alt="logo"/></td></tr><tr><td height="15"></td></tr><tr><td align="center" style="font-family:'Open Sans',Arial,sans-serif;font-size:12px;color:#3b3b3b;line-height:26px;text-transform:uppercase;letter-spacing:2px;font-weight:normal">开课吧 · 新职课 · 2021-08-05</td></tr><tr><td height="40"></td></tr><tr><td align="center" bgcolor="#f3f3f3"><table align="center" width="500" border="0" cellspacing="0" cellpadding="0"><tr><td height="50"></td></tr><tr><td align="center" style="font-family:'Open Sans',Arial,sans-serif;font-size:36px;color:#3b3b3b;font-weight:bold;letter-spacing:4px">无法验证邮箱地址</td></tr><tr><td align="center"><table width="25" border="0" cellspacing="0" cellpadding="0"><tr><td height="15" style="border-bottom:2px solid #3cb2d0"></td></tr></table></td></tr><tr><td height="20"></td></tr><tr><td align="center" style="font-family:'Open Sans',Arial,sans-serif;font-size:13px;color:#7f8c8d;line-height:30px">十分抱歉，我们缺少足够的信息来验证该邮件地址是否归属于该账号所有，请重新发送验证码。 如果您不知道为什么收到这封邮件，可能是其他人误输入了您的电子邮箱地址，这并没有关系，请忽略它。本邮件15分钟内有效。</td></tr><tr><td height="50"></td></tr></table></td></tr><tr><td height="40"></td></tr><tr><td align="center"><table align="center" bgcolor="#3cb2d0" border="0" cellspacing="0" cellpadding="0" style=" border-radius:4px;box-shadow:0 2px 0 #dedfdf"><tr><td height="55" align="center" style="cursor:pointer;font-family:'Open Sans',Arial,sans-serif;font-size:16px;color:#7f8c8d;line-height:30px;font-weight:bold;padding-left:25px;padding-right:25px"><a style="text-decoration:none;color:#fff;display:inline-block;width:75px;height:30px;padding:13px 20px 13px 20px;user-select:none;-webkit-user-select:none;-moz-user-select:none" href='" + localhost + "/email/index.do?code=" + code + "'>立即验证</a></td></tr></table></td></tr><tr><td align="center" style=" border-bottom-left-radius:4px;border-bottom-right-radius:4px"><table align="center" class="table-inner" width="290" border="0" cellspacing="0" cellpadding="0"><tr><td height="25"></td></tr><tr><td align="center"><table width="80" border="0" align="left" cellpadding="0" cellspacing="0"><tr><td align="center" style="font-family:'Open Sans',Arial,sans-serif;font-size:13px;color:#7f8c8d;line-height:30px"><a style="color:#95a5a6;text-decoration:none" href="http://666.zaixianke.cn" target="_blank">快递 e 栈</a></td></tr></table><table width="1" height="15" border="0" cellpadding="0" cellspacing="0" align="left"><tr><td height="15" style="font-size:0;line-height:0;border-collapse:collapse"><p style="padding-left:24px">&nbsp;</p></td></tr></table><table width="80" border="0" align="left" cellpadding="0" cellspacing="0"><tr><td align="center" style="font-family:'Open Sans',Arial,sans-serif;font-size:13px;color:#7f8c8d;line-height:30px"><a style="color:#95a5a6;text-decoration:none" href="https://www.kaikeba.com/">kaikeba</a></td></tr></table><table width="1" height="15" border="0" cellpadding="0" cellspacing="0" align="left"><tr><td height="15" style="font-size:0;line-height:0;border-collapse:collapse"><p style="padding-left:24px">&nbsp;</p></td></tr></table><table width="80" border="0" align="right" cellpadding="0" cellspacing="0"><tr><td align="center" style="font-family:'Open Sans',Arial,sans-serif;font-size:13px;color:#7f8c8d;line-height:30px"><a style="color:#95a5a6;text-decoration:none" href="http://666.zaixianke.cn">关于我们</a></td></tr></table></td></tr></table></td></tr><tr><td height="30"></td></tr></table></td></tr><tr><td height="30"></td></tr><tr><td align="center" style="height:60px;font-family:'Open Sans',Arial,sans-serif;font-size:13px;color:#ffffff;line-height:20px">© 2021 <a style="text-decoration:none;color:#1E9FFF" href="#">Liu Guangxian</a> . All Rights Reserved.</td></tr><tr><td height="35"></td></tr></table></td></tr></table>